<template>
  <el-dialog :title="title" :visible.sync="open" :width="width">
    <el-upload
      ref="upload"
      style="width: 100%"
      :limit="1"
      accept=".zip"
      :headers="upload.headers"
      :action="upload.url"
      :file-list="fileListAdd"
      :on-change="handleChange"
      :on-success="handleFileSuccess"
      :before-upload="beforeUpload"
      :auto-upload="false"
      drag
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text"><em>点击</em>或将文件拖拽到这里上传<br>支持扩展名.zip</div>
    </el-upload>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitFileForm">保 存</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from '@/utils/auth'

export default {
  props: {
    importData: {
      type: Object,
      default() {
        return {}
      }
    },
    width: {
      type: String,
      default() {
        return '400px'
      }
    },
    title: {
      type: String,
      default() {
        return '导入'
      }
    }
  },
  data() {
    return {
      // 是否显示弹出层（用户导入）
      open: false,
      upload: {},
      fileListAdd: []
    }
  },
  mounted() {
    this.upload = this.importData
  },
  methods: {
    /** 显示弹窗*/
    show() {
      this.open = true
      this.fileListAdd = []
    },
    //  文件状态改变时
    handleChange(file, fileList) {
      this.fileListAdd = fileList
    },
    beforeUpload() {},
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      console.log('文件上传中处理', fileList)
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      if (response.code == '200') {
        this.$message.success('导入成功')
        this.open = false
        this.$refs.upload.clearFiles()
        this.$emit('importFileSuccess')
      } else {
        this.$message.error(response.msg)
        this.$refs.upload.clearFiles()
      }

      /*     this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
        response.msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );*/
    },
    // 提交上传文件
    submitFileForm() {
      if (this.fileListAdd.length == 0) {
        this.$message.error('请选择上传文件')
      }
      this.$refs.upload.submit()
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/user/importTemplate', {}, `user_template_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style lang="scss" scoped>
// ::v-deep .el-dialog__body {
//   height: 50vh !important;
// }
::v-deep .el-upload {
  width: 100% !important;
}
::v-deep .el-upload-dragger {
  width: 100% !important;
}
::v-deep .el-dialog:not(.is-fullscreen) {
  height: auto !important;
}
</style>
